<?
class Item
{	
	static $id 	= -1, $item = false, $user = -1;
	
	const MAX_UPLOAD_SIZE = 1048576;//1M
	
	static function delete_item($id){
		// Delete logic
		$item 	= Item::get_item($id);
		$item_memcache 	= $item;
		
		if($item){
			if(User::is_admin() || User::have_permit(ADMIN_ITEM)) // check nguoi quan tri
			{
				DB::update('item',array('status'=>'-1','del_time'=>TIME_NOW,'del_user'=>User::user_name()),'id='.$id);
				
				$item_memcache['del_time'] 		= TIME_NOW;	
				$item_memcache['del_user'] 		= User::user_name();
			}
			else{
				DB::update('item',array('status'=>'-1','modify_time'=>TIME_NOW,'modify_user_name'=>User::user_name()),'id='.$id);
				
				$item_memcache['modify_time'] 		= TIME_NOW;	
				$item_memcache['modify_user_name'] 	= User::user_name();
			}
			
			if(MEMCACHE_ON){
				$item_memcache['status'] 			= -1;		
				AZMemcache::do_put("item:$id",$item_memcache);
			}
		}	
	}
	
	static function update_status($id, $status=1)
	{
		
		$item 			= Item::get_item($id);
	
		if($item && User::have_permit(ADMIN_ITEM))
		{	
			DB::query("UPDATE item SET status = $status, modify_time=".TIME_NOW.", modify_user_name = '".User::user_name()."' WHERE id = $id");
			
			if(MEMCACHE_ON)
			{
				$item_memcache 	= $item;
				$item_memcache['status'] 			= $status;	
				$item_memcache['modify_time'] 		= TIME_NOW;
				$item_memcache['modify_user_name'] 	= User::user_name();
				AZMemcache::do_put("item:$id",$item_memcache);
			}
		}
		
	}
	
	static function un_del_item($id){
		if(User::have_permit(ADMIN_ITEM)) // check nguoi quan tri
		{
			$item 			= Item::get_item($id);
		
			if($item)
			{	
				DB::query("UPDATE item SET status = 1 WHERE id = $id");
				
				if(MEMCACHE_ON)
				{
					$item_memcache 	= $item;
					$item_memcache['status'] 			= 1;

					AZMemcache::do_put("item:$id",$item_memcache);
				}
			}
		}
	}
	
	static function delete_item_physical($id){
		$item 	= Item::get_item($id);
		
		if($item && (User::is_admin() || $item['user_id']==User::id())){
			// xoa comment
			DB::update('item_image',array("item_id"=>0),"item_id=$id AND root_id=0");//Cập nhật các ảnh gốc
			DB::delete('item_image',"item_id=$id AND root_id>0");//Xoá các ảnh ko fải ảnh gốc
			DB::delete('item','id='.$id);
			
			//Loại item khỏi Memcache
			Item::get_item($id,1);
		}
	}
	
	static function get_time_list($type){
		switch($type){
			case 'hour':
				//$arr = array(''=>'Gi&#7901;');
				for($i=0;$i<=23;$i++){
					$arr[$i] = str_pad($i,2,'0',STR_PAD_LEFT);
				}
				return $arr;
			break;
			case 'minute':
				//$arr = array(''=>'Ph&#250;t');
				for($i=0;$i<=59;$i++){
					$arr[$i] = str_pad($i,2,'0',STR_PAD_LEFT);
				}
				return $arr;
			break;
			case 'day':
				//$arr = array(''=>'Ngày');
				for($i=1;$i<=31;$i++){
					$arr[$i] = str_pad($i,2,'0',STR_PAD_LEFT);
				}
				return $arr;
			break;
			case 'month':
				//$arr = array(''=>'Tháng');
				for($i=1;$i<=12;$i++){
					$arr[$i] = str_pad($i,2,'0',STR_PAD_LEFT);
				}
				return $arr;
			break;
			case 'year':
				//$arr = array(''=>'Năm');
				for($i=(date('Y',TIME_NOW));$i<=(date('Y',TIME_NOW)+1);$i++){
					$arr[$i] = $i;
				}
				return $arr;
			break;
		}
	}

	static function get_item($item_id,$delcache=0){
		$item=array();
				
		if($item_id){
			if($delcache){//Xoá cache
				if(MEMCACHE_ON){
					AZMemcache::do_remove("item:$item_id");
				}
				return true;
			}
			else{
				if(MEMCACHE_ON){
					$item = AZMemcache::do_get("item:$item_id");
				}
				
				if(!$item || (isset($_GET['delcache']) && $_GET['delcache'] == 1)){//Nếu item chưa đc cache thì lấy từ db ra
					$sql 	= "SELECT  * FROM item	WHERE 	id=$item_id";
					$item 	= mysql_fetch_assoc(DB::query($sql));
					
									
					if(MEMCACHE_ON && $item){
						AZMemcache::do_put("item:$item_id" , $item);
					}
				}
			}
		}
		return $item;
	}
	
	static function get_item_images($item_id, $del_cache=0){
		$sql		 = 'SELECT * FROM item_image WHERE item_id = '.$item_id.' ORDER BY position ';

		//$item_images = AZCache::cache($sql,__LINE__.__FILE__,1,0,'','',$del_cache);
		$item_images = AZCache::cache($sql,__LINE__.__FILE__,0,0,'','',$del_cache);
		
		if(!$del_cache)
		{
			$_item_images=false;
			
			if($item_images)
			{
				foreach($item_images as $value)
				{
					if($value && $value['img_url'])
					{
						$i	= $value['position'];
						$title	= str_replace(array('"',"'"),array('&quot;','\\\''),stripslashes($value['title']));
						
						$_item_images[$i]					= $value;
						$_item_images[$i]['i']				= $i;
						$_item_images[$i]['id']  			= $value['id'];
						$_item_images[$i]['title']  		= $title;
						$_item_images[$i]['img_server'] 	= $value['img_server'];
						$_item_images[$i]['thumb316']	 	= AZLib::getImageThumb($value['img_url'],316,0,0,  $value['img_server']);
						$_item_images[$i]['thumb80_80']	 	= AZLib::getImageThumb($value['img_url'],80,80,0,  $value['img_server']);
						$_item_images[$i]['img_full_url']	= "http://".CGlobal::$img_server[$value['img_server']].$value['img_url'];
						$_item_images[$i]['max_path']		= AZLib::getImageThumbMax($value['img_url'],$title,1,$value['img_server']);
					}
				}
				
				unset($item_images);								
				return $_item_images;
			}
			
			return array();
		}
		
		return true;
	}

	static function update_read_count()
	{
		if(Item::$item)
		{
			Item::$item['read_count']++;
			
			if(MEMCACHE_ON)
			{
				AZMemcache::do_put("item:".Item::$item['id'] , Item::$item);
						
				if(Item::$item['read_count'] % 10 == 0)
				{
					$sql_str = "UPDATE item SET read_count = read_count + 10 WHERE id = ".Item::$item['id'];
					DB::query($sql_str);		
				}
			}
			else
			{
				$sql_str = "UPDATE item SET read_count = read_count + 1 WHERE id = ".Item::$item['id'];
				DB::query($sql_str);
			}
		}
		/*
		DB::query("INSERT INTO eb_views(eb_id,eb_type,view_time) VALUES(".Item::$id.",0,".TIME_NOW.")");
		*/
	}
}
?>